#include "StringInput.h"



StringInput::StringInput(int x, int y, std::string prp)
{    
	prop = "";
	prop = prp;
    //Initialize the string
    value = "";
    
	font = TTF_OpenFont( "DroidSerif-Regular.ttf", 32 );

	textColor = new SDL_Color(); 
	textColor->b = 255;
	textColor->g = 255;
	textColor->r = 255;
    //Initialize the surface

          
    inner = new SDL_Rect();
	inner->h=32;
	inner->w = 300;
	inner->x = x;
	inner->y =y;
	outer = new SDL_Rect();
	outer->h = 36;
	outer->w = 304;
	outer->x = x-2;
	outer->y = y -2;
	isActive = false;
    
	//Enable Unicode
    SDL_EnableUNICODE( SDL_ENABLE );
	
            text = TTF_RenderText_Solid( font, "", *textColor );
}

StringInput::~StringInput()
{
    //Free text surface
   // SDL_FreeSurface( text );
    
    //Disable Unicode
    //SDL_EnableUNICODE( SDL_DISABLE );  

}

void StringInput::onClick()
{
	isActive=true;
}
void StringInput::onClickOut()
{

	isActive=false;
}

void StringInput::handleInput(SDL_Event event)
{ 
	isActive=true;

 
    if( event.type == SDL_KEYDOWN && isActive )
    {
			
        //Keep a copy of the current version of the string
        std::string temp = value; 
        //If the string less than maximum size
        if( value.length() <= 16 )
        { 
            //If the key is a space
            if( event.key.keysym.unicode == (Uint16)' ' )
            {
                //Append the character
                value += (char)event.key.keysym.unicode;    
            }//If the key is a number
			else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
            {
                //Append the character
                value += (char)event.key.keysym.unicode;
            }
            //If the key is a uppercase letter
            else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
            {
                //Append the character
                value += (char)event.key.keysym.unicode;
            }
            //If the key is a lowercase letter
            else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
            {
                //Append the character
                value += (char)event.key.keysym.unicode;
            }
        }

        //If backspace was pressed and the string isn't blank
        if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( value.length() != 0 ) )
        {
            //Remove a character from the end
            value.erase( value.length() - 1 );
        } 
        //If the string was changed
        if( value != temp )
        {
            //Free the old surface
            SDL_FreeSurface( text );
			
			
            //Render a new text surface
            text = TTF_RenderText_Solid( font, value.c_str(), *textColor );
        }
    }

	 
} 
void StringInput::show_centered(SDL_Surface* screen)  
{
    //If the surface isn't blank
    if( text != NULL )
    {
        //Show the name
        Draw::apply_surface( ( Draw::SCREEN_WIDTH - text->w ) / 2, ( Draw::SCREEN_HEIGHT - text->h ) / 2, text, screen );
    }
}

void StringInput::drawElement(SDL_Surface* screen)//text boxes are left justified. deal with it.
{
	
	int x = (inner->x +2  );
	int y = (inner->y +2 );
	if(isActive)
	{
	SDL_FillRect( screen, outer, SDL_MapRGB( screen->format, 0x00, 0xFF, 0x00 ) );
	}
	else
	{
	SDL_FillRect( screen, outer, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
	}
	SDL_FillRect( screen, inner, SDL_MapRGB( screen->format, 0x33, 0x33, 0x33 ) );
	//Draw::apply_surface( x, y, box, screen );
	 if( text != NULL )
    {
        //Show the name
        Draw::apply_surface( x, y, text, screen );
    }
}
 

std::string StringInput::getString()
{
	return value;
}